package com.my.main.service.userAndRight.serviceImpl;

import com.my.main.base.mapper.BaseMapper;
import com.my.main.base.service.Impl.BaseServiceImpl;
import com.my.main.common.utils.IdWorker;
import com.my.main.entity.userAndRight.SysRightsRouters;
import com.my.main.entity.userAndRight.SysRightsRoutersApi;
import com.my.main.mapper.userAndRight.SysRightsRoutersApiMapper;
import com.my.main.mapper.userAndRight.SysRightsRoutersMapper;
import com.my.main.service.userAndRight.SysRightsRoutersApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class SysRightsRoutersApiServiceImpl extends BaseServiceImpl<SysRightsRoutersApi> implements SysRightsRoutersApiService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private SysRightsRoutersMapper sysRightsRoutersMapper;
    @Autowired
    private SysRightsRoutersApiMapper sysRightsRoutersApiMapper;

    @Resource(name= "sysRightsRoutersApiMapper")
    public void setBaseMapper(BaseMapper<SysRightsRoutersApi> mapper) {
        super.setBaseMapper(mapper);
    }

    @Override
    public List<SysRightsRoutersApi> queryListByRouterId(Map<String, Object> map) {
        return sysRightsRoutersApiMapper.queryListByRouterId(map);
    }


    @Override
    public void isUpdateSysRightsRoutersApi(String routerId, List<String> apiList, String ip, String userId) {
        Map<String, Object> sqlMap = new HashMap<>();

        try {
            // 删除该路由所有映射
            sqlMap.clear();
            sqlMap.put("routerId", routerId);
            sysRightsRoutersApiMapper.isDelByRouterId(sqlMap);

            // 添加
            for(String api : apiList) {
                SysRightsRoutersApi srra = new SysRightsRoutersApi();
                srra.setRelationId(idWorker.nextId() + "");
                srra.setRouterId(routerId);
                srra.setApiUrl(api);
                srra.setAddIp(ip);
                srra.setAddUserId(userId);
                srra.setLastEditBy(userId);
                srra.setLastEditIp(ip);
                srra.setIsDel(false);
                sysRightsRoutersApiMapper.isSave(srra);
            }

            // 删除子路由所有映射
            sqlMap.clear();
            sqlMap.put("parentId", routerId);
            List<SysRightsRouters> sysRightsRoutersList = sysRightsRoutersMapper.queryListByMap(sqlMap);
            for(SysRightsRouters sysRightsRouters: sysRightsRoutersList) {
                sqlMap.clear();
                sqlMap.put("routerId", sysRightsRouters.getRouterId());
                sysRightsRoutersApiMapper.isDelByRouterId(sqlMap);

                // 添加子路由权限
                for(String api : apiList) {
                    SysRightsRoutersApi srra = new SysRightsRoutersApi();
                    srra.setRelationId(idWorker.nextId() + "");
                    srra.setRouterId(sysRightsRouters.getRouterId());
                    srra.setApiUrl(api);
                    srra.setAddIp(ip);
                    srra.setAddUserId(userId);
                    srra.setLastEditBy(userId);
                    srra.setLastEditIp(ip);
                    srra.setIsDel(false);
                    sysRightsRoutersApiMapper.isSave(srra);
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }


}
